<template>
	<view>
		<map enable-traffic
			:latitude="latitude"
			:longitude="longitude"
			:markers="markers"
			@tap="tapMap"></map>
		<input type="text"
			v-model.lazy="keyword"
			value=""
			@input="handleSearch" />
		<scroll-view scroll-y
			enable-flex
			class="search-list"
			v-if="searchList.length">
			<view class="list-item"
				v-for="item in searchList"
				:key="item.id">
				<text class="title">{{item.title}}</text>
				<text class="address">{{item.address}}</text>
			</view>
			<!-- 
			<view class="list-item" v-for="item in searchList" :key="item">
				{{item}}
			</view> -->
		</scroll-view>
	</view>
</template>

<script>
	import QQMapWX from '../../utils/qqmap-wx-jssdk.min.js';
	let qqmapsdk;

	// 防抖函数
	function debounce(func, delay) {
		let timer;
		return function() {
			let context = this;
			clearTimeout(timer);
			timer = setTimeout(function() {
				func.apply(context);
			}, delay);
		};
	}

	export default {
		data() {
			return {
				latitude: '39.909',
				longitude: '116.39742',
				markerLocation: {
					latitude: '39.909',
					longitude: '116.39742'
				},
				markers: [],
				newLocationId: 0,
				keyword: '',
				searchTimer: null,
				searchList: []
			};
		},
		onLoad() {
			this.getUserLocation();
			qqmapsdk = new QQMapWX({
				key: 'PT3BZ-PKXKS-LHPOL-6WWQP-PLCIQ-DIBSA'
			});
		},
		onShow() {},

		watch: {
			keyword(val) {
				// 调用接口
			}
		},
		methods: {
			generateLocationObj(id, latitude, longitude) {
				return {
					id,
					latitude,
					longitude,
					iconPath: '../../static/location.png',
					width: '80rpx',
					height: '80rpx'
				};
			},
			getUserLocation() {
				uni.getLocation({
					type: 'gcj02',
					success: res => {
						this.latitude = res.latitude;
						this.longitude = res.longitude;
						let currentLocation = this.generateLocationObj(this.newLocationId, res.latitude, res
							.longitude);
						this.markers.push(currentLocation);
					}
				});
			},
			handleSearch() {
				if (this.keyword) {
					if (this.searchTimer) {
						clearTimeout(this.searchTimer);
					}
					this.searchTimer = setTimeout(() => {
						qqmapsdk.search({
							keyword: this.keyword,
							success: ({ data }) => {
								this.searchList = [...data];
								console.log(data);
							},
							fail: function(res) {
								console.log(res);
							}
						});
					}, 2000);
				}
			},



			tapMap(e) {
				console.log(e);
				// this.newLocationId++;
				// let newLocation = this.generateLocationObj(this.newLocationId,e.detail.latitude,e.detail.longitude);
				//  this.markers.push(
				// 	newLocation
				//  );
			}
		}
	};
</script>

<style lang="scss"
	scoped>
	page {
		position: relative;
	}

	map {
		position: relative;
		z-index: 0;
		width: 750rpx;
		height: 100vh;
	}

	input {
		position: absolute;
		top: 40rpx;
		left: 0;
		right: 0;
		bottom: 0;
		margin: 0 auto;
		padding: 0 20rpx;
		z-index: 100;
		width: 500rpx;
		height: 60rpx;
		border-radius: 10rpx;
		box-shadow: $uni-box-shadow;
		background-color: #fff;
	}

	.search-list {
		width: 540rpx;
		height: 700rpx;
		min-height: 0rpx;
		position: absolute;
		top: 120rpx;
		left: 0;
		right: 0;
		bottom: 0;
		margin: 0 auto;
		z-index: 100;
		box-shadow: $uni-box-shadow;
		border-radius: 10rpx;
		overflow: hidden;

		.list-item {
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;
			align-items: center;
			box-sizing: border-box;
			width: 540rpx;
			height: 140rpx;
			// line-height: 60rpx;
			color: #777777;
			background-color: #fff;
			font-size: 32rpx;
			padding: 20rpx 20rpx;
			border-bottom: 2rpx solid #e3e3e3;

			text {
				width: 100%;

				&.title {
					font-size: 28rpx;
					text-align: left;
				}

				&.address {
					font-size: 24rpx;
					color: #888888;
				}

			}
		}
	}
</style>
